package cn.springcloud.book.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
public class TestController {
	@Value("${security.oauth2.resource.jwt.key-value}")
	private String jwtkey;

	@GetMapping("/token")
	public String token(HttpServletRequest request){
		String authorization = request.getHeader("Authorization");
		String token = authorization.replace("bearer ", "");
		Claims claims = Jwts.parser().setSigningKey(jwtkey.getBytes(StandardCharsets.UTF_8)).parseClaimsJws(token).getBody();
		Object username = claims.get("user_name");
		System.out.println(token);
		System.out.println(claims);
		System.out.println(username);

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		System.out.println(principal);
		return authorization;
	}

}
